﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;
using Assist;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Revit;
using YArchitech.Revit.RevitExtension;

namespace YJKArch.DimensionModule
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	[Journaling(JournalingMode.NoCommandData)]
	public class CmdCoordinateDim : YJKCommandManual
	{
		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		protected override Result YJKExcute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			if (CmdCoordinateDim._hWndRevit == null)
			{
				Process[] processesByName = Process.GetProcessesByName("Revit");
				if (processesByName.Length != 0)
				{
					CmdCoordinateDim._hWndRevit = new WindowHandle(processesByName[0].MainWindowHandle);
				}
			}
			UIDocument activeUIDocument = commandData.Application.ActiveUIDocument;
			Autodesk.Revit.DB.Document document = activeUIDocument.Document;
			Autodesk.Revit.DB.View activeView = document.ActiveView;
			DimDate.FontFamily = AssistFunc.GetSystemFontTypeStringArray(document);
			IEnumerable<Element> enumerable = new FilteredElementCollector(document).OfClass(typeof(Autodesk.Revit.DB.ElementType)).ToElements();
			List<string> list = new List<string>();
			foreach (Element element in enumerable)
			{
				Autodesk.Revit.DB.ElementType elementType = (Autodesk.Revit.DB.ElementType)element;
				if (elementType.get_Parameter(BuiltInParameter.ALL_MODEL_FAMILY_NAME).AsString() == "箭头")
				{
					list.Add(elementType.Name);
				}
			}
			DimDate.ArrowType = list;
			DlgCoordinateDim dlgCoordinateDim = new DlgCoordinateDim();
			dlgCoordinateDim.Show(CmdCoordinateDim._hWndRevit);
			int wMsg = 7;
			CmdCoordinateDim.SendMessage(CmdCoordinateDim._hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
			XYZ xyz = null;
			XYZ xyz2 = null;
			string text = null;
			string strText = null;
			Line line = null;
			Result result;
			for (;;)
			{
				ElementSet elementSet = new ElementSet();
				Transaction transaction = new Transaction(document);
				transaction.Start("revit");
				try
				{
					xyz = activeUIDocument.Selection.PickPoint("请选择一个点");
					dlgCoordinateDim.Enabled = false;
					xyz2 = activeUIDocument.Selection.PickPoint("请选择一个点");
					transaction.Commit();
				}
				catch (Exception)
				{
					transaction.RollBack();
					if (dlgCoordinateDim.Enabled)
					{
						dlgCoordinateDim.Close();
						result = Autodesk.Revit.UI.Result.Succeeded;
						break;
					}
					dlgCoordinateDim.Enabled = true;
					continue;
				}
				line = YJKLineEx.YJKGetBound(xyz, xyz2);
				XYZ xyz3 = xyz2 - xyz;
				Element selectType = Helper.GetSelectType<Autodesk.Revit.DB.ElementType>(document, dlgCoordinateDim.ArrowType);
				Transaction transaction2 = new Transaction(document);
				transaction2.Start("rev");
				XYZ xyz4 = null;
				try
				{
					if (dlgCoordinateDim.TXTFixAngle.Trim() != "")
					{
						XYZ vect = xyz - new XYZ(0.0, xyz.Y, 1.0);
						Line line2 = YJKLineEx.YJKGetBound(xyz, new XYZ(0.0, xyz.Y, 1.0));
						double num = double.Parse(dlgCoordinateDim.TXTFixAngle);
						XYZ xyz5 = Helper.Rotate(vect, new XYZ(0.0, xyz.Y, 1.0), num * Math.PI / 180.0);
						xyz4 = new XYZ(0.0, xyz.Y, 1.0) + xyz5.Normalize() * line2.Length;
					}
					else
					{
						xyz4 = xyz;
					}
				}
				catch (Exception)
				{
					transaction2.RollBack();
					dlgCoordinateDim.Close();
					result = Autodesk.Revit.UI.Result.Succeeded;
					break;
				}
				if (dlgCoordinateDim.ischeckrdo)
				{
					text = "X=" + xyz4.X.ToString(dlgCoordinateDim.TagPrecision);
					strText = "Y=" + xyz4.Y.ToString(dlgCoordinateDim.TagPrecision);
				}
				else
				{
					text = "A=" + xyz4.X.ToString(dlgCoordinateDim.TagPrecision);
					strText = "B=" + xyz4.Y.ToString(dlgCoordinateDim.TagPrecision);
				}
				double num2 = (double)Encoding.Default.GetBytes(text).Length;
				Element selectType2 = Helper.GetSelectType<TextNoteType>(document, dlgCoordinateDim.FontType);
				double num3 = Helper.FontSize(selectType2 as TextNoteType) * (double)activeView.Scale / 100.0;
				double num4 = selectType2.get_Parameter(BuiltInParameter.TEXT_SIZE).AsDouble() * (double)activeView.Scale;
				XYZ baseVec = null;
				XYZ upVec = null;
				TextNote textNote = null;
				if (xyz2 != null)
				{
					new XYZ(xyz2.X, xyz2.Y, xyz2.Z);
					baseVec = new XYZ(1.0, 0.0, 0.0);
					upVec = new XYZ(0.0, 0.0, 1.0);
				}
				try
				{
                    //TextNoteLeaderTypes leaderType, TextNoteLeaderStyles leaderStyle,

                    textNote = document.GetTextNode(document.ActiveView, xyz2, baseVec, upVec, 1.0, (TextAlignFlags)64, (TextNoteLeaderTypes)0,(TextNoteLeaderStyles)1, xyz, xyz2, ".");
					elementSet.Insert(textNote);
				}
				catch (Exception)
				{
				}
				XYZ origin = new XYZ(xyz2.X, xyz2.Y + num4, xyz2.Z);
				XYZ origin2 = new XYZ(xyz2.X, xyz2.Y, xyz2.Z);
				double num5 = xyz3.AngleTo(new XYZ(1.0, 0.0, 0.0));
				XYZ xyz6 = null;
				double num6 = 0.0;
				try
				{
					num6 = double.Parse(dlgCoordinateDim.Fixangle);
				}
				catch (Exception)
				{
				}
				if (dlgCoordinateDim.isFixangle)
				{
					if (xyz3.X >= 0.0 && xyz3.Y >= 0.0)
					{
						xyz6 = Helper.Rotate(xyz3, new XYZ(0.0, 0.0, 1.0), -(num5 - num6 * Math.PI / 180.0));
					}
					else if (xyz3.X <= 0.0 && xyz3.Y <= 0.0)
					{
						xyz6 = Helper.Rotate(xyz3, new XYZ(0.0, 0.0, 1.0), num5 + num6 * Math.PI / 180.0 + Math.PI);
					}
					else if (xyz3.X >= 0.0 && xyz3.Y <= 0.0)
					{
						xyz6 = Helper.Rotate(xyz3, new XYZ(0.0, 0.0, 1.0), num5 - num6 * Math.PI / 180.0);
					}
					else if (xyz3.X <= 0.0 && xyz3.Y >= 0.0)
					{
						xyz6 = Helper.Rotate(xyz3, new XYZ(0.0, 0.0, 1.0), -(num5 + num6 * Math.PI / 180.0 + Math.PI));
					}
					xyz2 = xyz + xyz6.Normalize() * line.Length;
				}
				if ((xyz3.X >= 0.0 && xyz3.Y >= 0.0) || (xyz3.X >= 0.0 && xyz3.Y <= 0.0))
				{
					XYZ ptCoord = new XYZ(xyz2.X + num2 * num3 / 1.7, xyz2.Y + 0.6 * (double)activeView.Scale / 100.0, xyz2.Z);
					RevitElementExtension.SetTextNoteCoord(textNote, ptCoord);
					origin = new XYZ(xyz2.X, xyz2.Y + num4 + 0.4 * (double)activeView.Scale / 100.0, xyz2.Z);
					origin2 = new XYZ(xyz2.X, xyz2.Y - 0.2 * (double)activeView.Scale / 100.0, xyz2.Z);
				}
				else if ((xyz3.X <= 0.0 && xyz3.Y <= 0.0) || (xyz3.X <= 0.0 && xyz3.Y >= 0.0))
				{
					XYZ ptCoord2 = new XYZ(xyz2.X - num2 * num3 / 1.7, xyz2.Y + 0.6 * (double)activeView.Scale / 100.0, xyz2.Z);
					RevitElementExtension.SetTextNoteCoord(textNote, ptCoord2);
					origin = new XYZ(xyz2.X - num2 * num3 / 1.7, xyz2.Y + num4 + 0.4 * (double)activeView.Scale / 100.0, xyz2.Z);
					origin2 = new XYZ(xyz2.X - num2 * num3 / 1.7, xyz2.Y - 0.2 * (double)activeView.Scale / 100.0, xyz2.Z);
				}
				RevitElementExtension.SetTextNoteWidth(textNote, (double)textNote.Text.Length * num3);
				textNote.TextNoteType.get_Parameter(BuiltInParameter.LEADER_ARROWHEAD).Set(selectType.Id);
				textNote.HyGetLeaders().get_Item(0).Elbow = xyz2;
                TextNote textNode = document.GetTextNode(document.ActiveView, origin, baseVec, upVec, 0.0001, (TextAlignFlags)64, text);
				textNode.TextNoteType = (selectType2 as TextNoteType);
				RevitElementExtension.SetTextNoteWidth(textNode, num2 * num3);
				elementSet.Insert(textNode);
                TextNote textNode2 = document.GetTextNode(document.ActiveView, origin2, baseVec, upVec, 0.0001, (TextAlignFlags)64, strText);
				textNode2.TextNoteType = (selectType2 as TextNoteType);
				RevitElementExtension.SetTextNoteWidth(textNode2, num2 * num3);
				elementSet.Insert(textNode2);
				transaction2.Commit();
				Transaction transaction3 = new Transaction(document, "group");
				transaction3.Start();
				RevitVersionFuncs.NewGroup(document, elementSet);
				transaction3.Commit();
			}
			return result;
		}

		public static WindowHandle _hWndRevit;
	}
}
